package cn.edu.sfu.financial.web.action

import java.io.File
import org.beangle.commons.collection.Order
import org.beangle.data.jpa.dao.OqlBuilder
import org.beangle.data.model.dao.EntityDao
import org.beangle.webmvc.api.annotation.mapping
import org.beangle.webmvc.api.annotation.param
import org.beangle.webmvc.api.view.Stream
import org.beangle.webmvc.api.view.View
import org.beangle.webmvc.entity.action.AbstractEntityAction
import org.openurp.edu.student.info.model.Caze
import org.openurp.edu.student.info.model.Policy
import org.openurp.edu.student.info.model.Paper
import org.openurp.edu.student.info.model.Newz
import org.openurp.edu.student.info.model.IndexBase
import org.openurp.edu.student.info.model.Example
import org.openurp.edu.student.info.model.Paper
/**
 * @author hjc54_000
 */
class IndexAction(entityDao: EntityDao) extends AbstractEntityAction[Policy] {
  def index(): String = {
    get("key") foreach { k =>
      put("key", k)
    }
    forward()
  }

  def policies(): String = {
    val builder = OqlBuilder.from(classOf[Policy], "policy")
    populateConditions(builder) //根据参数构建where语句
    builder.orderBy(get(Order.OrderStr).orNull).limit(getPageLimit) //排序和分页 limit 分页，hql语句

    val begin = getInt("begin")
    val end = getInt("end")
    if (begin.isDefined && end.isEmpty) {
      builder.where("year(policy.docdate) >= :begin", begin.get);
    } else if (begin.isEmpty && end.isDefined) {
      builder.where("year(policy.docdate) <= :end", end.get);
    } else if (begin.isDefined && end.isDefined) {
      builder.where("year(policy.docdate) between :begin and :end", begin.get, end.get);
    
    get("policyscope") foreach { policyscope =>
      if (policyscope.length > 0)
        builder.where("policies.policyscope.name = :policyscopeName", policyscope)
    }
    }

    //    builder.where("policy.policyscope.name = '全国'")
    val policies = entityDao.search(builder)
    put("policies", policies)

    builder.limit(null)
    builder.select("year(policy.docdate),count(*)").groupBy("year(policy.docdate)")

    put("yearStats", entityDao.search(builder))

    builder.clearGroupBys()
    builder.select("policy.policyscope.name,count(*)").groupBy("policy.policyscope.name")
    put("policyScopeStats", entityDao.search(builder));

    forward()
  }

  def cazes(): String = {
    val builder = OqlBuilder.from(classOf[Caze], "caze")
    populateConditions(builder) //根据参数构建where语句
    builder.orderBy(get(Order.OrderStr).orNull).limit(getPageLimit)
    val begin = getInt("begin")
    val end = getInt("end")
    if (begin.isDefined && end.isEmpty) {
      builder.where("caze.year >= :begin", begin.get);
    } else if (begin.isEmpty && end.isDefined) {
      builder.where("caze.year <= :end", end.get);
    } else if (begin.isDefined && end.isDefined) {
      builder.where("caze.year between :begin and :end", begin.get, end.get);
    }
    put("cazes", entityDao.search(builder)) //排序和分页 limit 分页，hql语句
    builder.limit(null)
    builder.select("caze.year,count(*)").groupBy("caze.year")
    put("yearStats", entityDao.search(builder))
    forward()
  }

  def examples(): String = {
    val builder = OqlBuilder.from(classOf[Example], "example")
    populateConditions(builder) //根据参数构建where语句
    builder.orderBy(get(Order.OrderStr).orNull).limit(getPageLimit)
    val begin = getInt("begin")
    val end = getInt("end")
    if (begin.isDefined && end.isEmpty) {
      builder.where("example.year >= :begin", begin.get);
    } else if (begin.isEmpty && end.isDefined) {
      builder.where("example.year <= :end", end.get);
    } else if (begin.isDefined && end.isDefined) {
      builder.where("example.year between :begin and :end", begin.get, end.get);
    }
    get("division") foreach { division =>
      if (division.length > 0)
        builder.where("example.division.name = :divisionName", division)
    }
     get("projecttype") foreach { projecttype =>
      if (projecttype.length > 0){
        builder.where("example.projecttype2.name like :projecttypeName", projecttype.trim()+"%")
      }
    }
    put("examples", entityDao.search(builder)) //排序和分页 limit 分页，hql语句

    builder.limit(null)
    builder.select("example.year,count(*)").groupBy("example.year")
    put("yearStats", entityDao.search(builder))

    builder.clearGroupBys()
    builder.select("example.division.name,count(*)").groupBy("example.division.name")
    put("divisionStats", entityDao.search(builder));

    builder.clearGroupBys()
    builder.select("example.projecttype2.name,count(*)").groupBy("example.projecttype2.name")
    put("projecttype2Stats", entityDao.search(builder));

    forward()
  }

  def indexbases(): String = {
    val builder = OqlBuilder.from(classOf[IndexBase], "indexbase")
    populateConditions(builder) //根据参数构建where语句
    builder.orderBy(get(Order.OrderStr).orNull).limit(getPageLimit)
    val begin = getInt("begin")
    val end = getInt("end")
    if (begin.isDefined && end.isEmpty) {
      builder.where("indexbase.year >= :begin", begin.get);
    } else if (begin.isEmpty && end.isDefined) {
      builder.where("indexbase.year <= :end", end.get);
    } else if (begin.isDefined && end.isDefined) {
      builder.where("indexbase.year between :begin and :end", begin.get, end.get);
    }
     get("industry") foreach { industry =>
      if (industry.length > 0)
        builder.where("indexbase.industry.name = :industryName", industry)
    }
     get("projecttype") foreach { projecttype =>
      if (projecttype.length > 0){
        builder.where("indexbase.projecttype.name like :projecttypeName", projecttype.trim()+"%")
      }
    }
    put("indexbases", entityDao.search(builder)) //排序和分页 limit 分页，hql语句

    builder.limit(null)
    builder.select("indexbase.year,count(*)").groupBy("indexbase.year")
    put("yearStats", entityDao.search(builder))
    builder.clearGroupBys()

    builder.select("new map(indexbase.industry.name,count(*))").groupBy("indexbase.industry.name")
    put("industryStats", entityDao.search(builder))

    builder.clearGroupBys()
    builder.select("indexbase.projecttype.name,count(*)").groupBy("indexbase.projecttype.name")
    put("projectTypeStats", entityDao.search(builder));
    forward()
  }
  def papers(): String = {
    val builder = OqlBuilder.from(classOf[Paper], "paper")
    populateConditions(builder) //根据参数构建where语句
    builder.orderBy(get(Order.OrderStr).orNull).limit(getPageLimit)
    val papers = entityDao.search(builder)
    put("papers", papers) //排序和分页 limit 分页，hql语句
    forward()
  }

  def newzs(): String = {

    val builder = OqlBuilder.from(classOf[Newz], "newz")
    populateConditions(builder)
    builder.orderBy(get(Order.OrderStr).orNull).limit(getPageLimit)
    val newzs = entityDao.search(builder)
    put("newzs", newzs)
    forward()
  }

  @mapping("policy/{id}")
  def policyInfo(@param("id") policyId: String): String = {
    val policy = entityDao.get(classOf[Policy], java.lang.Long.valueOf(policyId))
    put("policy", policy)
    forward()
  }

  @mapping("example/{id}")
  def exampleInfo(@param("id") exampleId: String): String = {
    val example = entityDao.get(classOf[Example], java.lang.Long.valueOf(exampleId))
    put("example", example)
    forward()
  }
  @mapping("caze/{id}")
  def cazeInfo(@param("id") cazeId: String): String = {
    val caze = entityDao.get(classOf[Caze], java.lang.Long.valueOf(cazeId))
    put("caze", caze)
    forward()
  }
  @mapping("indexbase/{id}")
  def indexbaseInfo(@param("id") indexbaseId: String): String = {
    val indexbase = entityDao.get(classOf[IndexBase], java.lang.Long.valueOf(indexbaseId))
    put("indexbase", indexbase)
    forward()
  }

  @mapping("paper/{id}")
  def paperInfo(@param("id") paperId: String): String = {
    val paper = entityDao.get(classOf[Paper], java.lang.Long.valueOf(paperId))
    put("paper", paper)
    forward()
  }

  @mapping("newz/{id}")
  def newzInfo(@param("id") newzId: String): String = {
    val newz = entityDao.get(classOf[Paper], java.lang.Long.valueOf(newzId))
    put("newz", newz)
    forward()
  }

  def filePolicy(@param("id") policyId: String): View = {
    val policy = entityDao.get(classOf[Policy], java.lang.Long.valueOf(policyId))
    Stream(new File("/home/openurp/financial_files/" + policy.code + ".pdf"))
  }

  def viewPolicy(@param("id") policyId: String): String = {
    val policy = entityDao.get(classOf[Policy], java.lang.Long.valueOf(policyId))
    if (null != policy) {
      val file = new File("/home/openurp/financial_files/" + policy.code + ".pdf")
      if (file.exists) put("policy", policy)
    }
    forward()
  }
  def fileExample(@param("id") exampleId: String): View = {
    val example = entityDao.get(classOf[Example], java.lang.Long.valueOf(exampleId))
    Stream(new File("/home/openurp/financial_files/" + example.code + ".pdf"))
  }

  def viewExample(@param("id") exampleId: String): String = {
    val example = entityDao.get(classOf[Example], java.lang.Long.valueOf(exampleId))
    if (null != example) {
      val file = new File("/home/openurp/financial_files/" + example.code + ".pdf")
      if (file.exists) put("example", example)
    }
    forward()
  }

  def fileIndexBase(@param("id") indexbaseId: String): View = {
    val indexbase = entityDao.get(classOf[IndexBase], java.lang.Long.valueOf(indexbaseId))
    Stream(new File("/home/openurp/financial_files/" + indexbase.code + ".pdf"))
  }

  def viewIndexBase(@param("id") indexbaseId: String): String = {
    val indexbase = entityDao.get(classOf[IndexBase], java.lang.Long.valueOf(indexbaseId))
    if (null != indexbase) {
      val file = new File("/home/openurp/financial_files/" + indexbase.code + ".pdf")
      if (file.exists) put("indexbase", indexbase)
    }
    forward()
  }
  
  def filePaper(@param("id") paperId: String): View = {
    val paper = entityDao.get(classOf[Paper], java.lang.Long.valueOf(paperId)) 
    Stream(new File("/home/openurp/financial_files/" + paper.code + ".pdf"))
  }

  def viewPaper(@param("id") paperId: String): String = {
    val paper = entityDao.get(classOf[Paper], java.lang.Long.valueOf(paperId))
    if (null != paper) {
      val file = new File("/home/openurp/financial_files/" + paper.code + ".pdf")
      if (file.exists) put("paper", paper)
    }
    forward()
  }

}